package com.project;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/7/19 15:47
 */
@Configuration
@ComponentScan(basePackageClasses = MyBatisConfig.class)
@MapperScan("com.project.mapper")
@EnableAspectJAutoProxy
public class MyBatisConfig {

    @Bean
    public DataSource getDateSource(){
        DruidDataSource source=new DruidDataSource();
        source.setDriverClassName("com.mysql.jdbc.Driver");
        source.setUrl("jdbc:mysql://localhost:1234/testDB?characterEncoding=utf-8&allowMultiQueries=true");
        source.setUsername("root");
        source.setPassword("lovo");
        source.setMaxActive(100);
        source.setMinIdle(20);
        source.setMaxActive(1000);
        return source;
    }

    @Bean
    public SqlSessionFactoryBean getFactoryBean(){
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(this.getDateSource());
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis.cfg.xml"));
        return sqlSessionFactoryBean;
    }

    @Bean
    public TransactionManager getManager(){
        DataSourceTransactionManager trans=new DataSourceTransactionManager();
        trans.setDataSource(this.getDateSource());
        return trans;
    }
}
